﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.Authority
{
    /// <summary>
    /// 功能菜单管理器
    /// </summary>
    public class FunctionManager
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="parentFuncGuid">父节点</param>
        /// <param name="funcDesc">菜单说明</param>
        /// <param name="funcUrl">Url地址</param>
        /// <param name="funcIcon">菜单图标</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Add(string parentFuncGuid, string funcDesc, string funcUrl, string funcIcon, int seq)
        {
            try
            {
                string sql = string.Empty;
                string funcGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                sql = "Insert Into InfoCodeFunc (FuncGuid,ParentFuncGuid,FuncDesc,FuncUrl,FuncIcon,Seq) Values(@FuncGuid,@ParentFuncGuid,@FuncDesc,@FuncUrl,@FuncIcon,@Seq)";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@FuncGuid", funcGuid),
                    SqlHelper.NewSqlParameter("@ParentFuncGuid", parentFuncGuid),
                    SqlHelper.NewSqlParameter("@FuncDesc", funcDesc),
                    SqlHelper.NewSqlParameter("@FuncUrl", funcUrl),
                    SqlHelper.NewSqlParameter("@FuncIcon", funcIcon),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="funcGuid">菜单Guid</param>
        /// <returns>是否成功</returns>
        public bool Delete(string funcGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Delete From InfoCodeFunc Where FuncGuid=@FuncGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@FuncGuid", funcGuid));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="funcGuid">菜单Guid</param>
        /// <param name="funcDesc">功能说明</param>
        /// <param name="funcUrl">Url地址</param>
        /// <param name="funcIcon">菜单图标</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Update(string funcGuid, string funcDesc, string funcUrl, string funcIcon, int seq)
        {
            try
            {
                string sql = string.Empty;
                sql = "Update InfoCodeFunc Set FuncDesc=@FuncDesc,FuncUrl=@FuncUrl,FuncIcon=@FuncIcon,Seq=@Seq Where FuncGuid=@FuncGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@FuncGuid", funcGuid),
                    SqlHelper.NewSqlParameter("@FuncDesc", funcDesc),
                    SqlHelper.NewSqlParameter("@FuncUrl", funcUrl),
                    SqlHelper.NewSqlParameter("@FuncIcon", funcIcon),
                    SqlHelper.NewSqlParameter("@Seq", seq));

                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取菜单列表（数据集）
        /// </summary>
        /// <param name="parentFuncGuid">父节点</param>
        /// <returns></returns>
        public DataSet GetFunctionDs(string parentFuncGuid)
        {
            try
            {
                string sql = "Select * From InfoCodeFunc Where ParentFuncGuid=@ParentFuncGuid Order by Seq Asc";
                return SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@ParentFuncGuid", parentFuncGuid));
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取菜单列表（数组）
        /// </summary>
        /// <param name="parentFuncGuid">父节点</param>
        /// <returns></returns>
        public List<Function> GetFunctionList(string parentFuncGuid)
        {
            try
            {
                List<Function> list = new List<Function>();
                DataSet ds = this.GetFunctionDs(parentFuncGuid);
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        list.Add(new Function(row["FuncGuid"].ToString(), row["ParentFuncGuid"].ToString(), row["FuncDesc"].ToString(), row["FuncUrl"].ToString(), row["FuncIcon"].ToString(), int.Parse(row["Seq"].ToString())));
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 判断是否有子菜单
        /// </summary>
        /// <param name="funcGuid">菜单Guid</param>
        /// <returns></returns>
        public bool ExistChild(string funcGuid)
        {
            try
            {
                string sql = "Select FuncGuid From InfoCodeFunc Where ParentFuncGuid=@ParentFuncGuid";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@ParentFuncGuid", funcGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取多级菜单的XML文本
        /// </summary>
        /// <returns></returns>
        public XmlDocument GetFunctionXmlDoc()
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelement;

            xmldoc = new XmlDocument();
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            xmlelement = xmldoc.CreateElement("root");
            xmldoc.AppendChild(xmlelement);

            this.RecursionFunction(ref xmldoc, ref xmlelement, "0");

            return xmldoc;
        }

        /// <summary>
        /// 递归多级菜单
        /// </summary>
        /// <param name="xmldoc">XML文本</param>
        /// <param name="parentXmlElement">XML父节点</param>
        /// <param name="parentFuncGuid">父节点</param>
        private void RecursionFunction(ref XmlDocument xmldoc, ref XmlElement parentXmlElement, string parentFuncGuid)
        {
            XmlElement xmlelement;
            List<Function> list = this.GetFunctionList(parentFuncGuid);
            foreach (Function function in list)
            {
                xmlelement = xmldoc.CreateElement("root_" + function.FuncGuid);
                xmlelement.SetAttribute("id", function.FuncGuid);
                xmlelement.SetAttribute("desc", function.FuncDesc);
                xmlelement.SetAttribute("icon", function.FuncIcon);
                xmlelement.SetAttribute("url", function.FuncUrl);
                parentXmlElement.AppendChild(xmlelement);

                this.RecursionFunction(ref xmldoc, ref xmlelement, function.FuncGuid);
            }
        }
    }
}
